<template>
  <div class="dashboard-container">
    <!-- 待办事项卡片 -->
    <el-card class="todo-container">
      <div slot="header" class="card-header">
        <div class="header-content">
          <i class="el-icon-bell header-icon"></i>
          <span class="header-title">待办事项</span>
          <span class="todo-count">
            {{ filteredTodos.length }}
            {{ currentTab === 'processing' ? '条待处理' : '条事项' }}
          </span>
        </div>
      </div>
      <div class="tab-bar">
        <span class="tab-item" :class="{ active: currentTab === 'all' }" @click="currentTab = 'all'">全部</span>
        <span class="tab-item" :class="{ active: currentTab === 'processing' }"
          @click="currentTab = 'processing'">待处理</span>
        <span class="tab-item" :class="{ active: currentTab === 'processed' }"
          @click="currentTab = 'processed'">已处理</span>
      </div>
      <ul class="todo-list">
        <li v-for="item in filteredTodos" :key="item.id" class="todo-item">
          <span class="status-tag" :class="item.status === 'processing' ? 'processing' : 'processed'">
            [{{ item.status === 'processing' ? '待处理' : '已处理' }}]
          </span>
          <span class="content">{{ item.content }}</span>
          <span v-if="item.status === 'processing'" class="handle-btn" @click="handleProcess(item)">去处理</span>
          <span v-else class="handle-btn" @click="handleViewTodoDetails(item)">查看详细</span>
          <span class="time">{{ item.time }}</span>
        </li>
      </ul>
    </el-card>

    <!-- 合作通知卡片 -->
    <el-card class="todo-container">
      <div slot="header" class="card-header">
        <div class="header-content">
          <i class="el-icon-chat-dot-square header-icon"></i>
          <span class="header-title">合作通知</span>
          <span class="todo-count">
            {{ filteredNotifications.length }}
            {{ notificationTab === 'unread' ? '条未读' : (notificationTab === 'read' ? '条已读' : '条通知') }}
          </span>
        </div>
      </div>
      <div class="tab-bar">
        <span class="tab-item" :class="{ active: notificationTab === 'all' }" @click="notificationTab = 'all'">全部</span>
        <span class="tab-item" :class="{ active: notificationTab === 'read' }"
          @click="notificationTab = 'read'">已读</span>
        <span class="tab-item" :class="{ active: notificationTab === 'unread' }"
          @click="notificationTab = 'unread'">未读</span>
      </div>
      <ul class="todo-list">
        <li v-for="item in filteredNotifications" :key="item.id" class="todo-item">
          <span class="status-tag" :class="item.status === 'unread' ? 'unread' : 'read'">
            [{{ item.status === 'unread' ? '未读' : '已读' }}]
          </span>
          <span class="content">{{ item.content }}</span>
          <span v-if="item.status === 'unread'" class="handle-btn" @click="handleMarkAsRead(item)">标记为已读</span>
          <span v-else class="handle-btn" @click="handleViewNotificationDetails(item)">查看详情</span>
          <span class="time">{{ item.time }}</span>
        </li>
      </ul>
    </el-card>

    <!-- 处理对话框 -->
    <el-dialog title="处理待办事项" :visible.sync="processingDialogVisible" width="600px" @close="resetProcessingForm">
      <el-form ref="processForm" :model="currentTodo">
        <el-form-item label="事项内容">
          <span>{{ currentTodo.content }}</span>
        </el-form-item>
      </el-form>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="processingDialogVisible = false">同意</el-button>
          <el-button type="primary" @click="submitProcessing">拒绝</el-button>
        </span>
      </template>
    </el-dialog>

    <!-- 详情对话框 -->
    <el-dialog title="事项详情" :visible.sync="detailDialogVisible" width="600px">
      <div class="detail-content">
        <el-row>
          <el-col :span="8">事项内容:</el-col>
          <el-col :span="16">{{ currentTodo.content }}</el-col>
        </el-row>
        <el-row>
          <el-col :span="8">状态:</el-col>
          <el-col :span="16">{{ currentTodo.status === 'processing' ? '待处理' : '已处理' }}</el-col>
        </el-row>
        <el-row>
          <el-col :span="8">创建时间:</el-col>
          <el-col :span="16">{{ currentTodo.time }}</el-col>
        </el-row>
        <el-row v-if="currentTodo.status === 'processed'">
          <el-col :span="8">处理记录:</el-col>
          <el-col :span="16">
            处理人: {{ currentTodo.handler }}<br>
            处理时间: {{ currentTodo.processTime }}<br>
            处理意见: {{ currentTodo.remark }}
          </el-col>
        </el-row>
      </div>
    </el-dialog>
  </div>
</template>
<script>
export default {
  data() {
    return {
      currentTab: 'all',
      todos: [
        { status: 'processing', content: '成员【节点03】邀请您加入项目【CNN模型训练1151、EMNIST】', time: '2025-01-05 15:22' },
        { status: 'processed', content: '成员【节点07】邀请您加入项目【快捷项目-20250328164825】', time: '2025-03-27 21:48:10' },
        { status: 'processing', content: '成员【节点05】邀请您加入项目【CNN模型训练1151、EMNIST】', time: '2025-01-06 08:43' },
        // { status: 'processing', content: '成员【数据提供商一】申请使用结构化数据集：lb-测试数据A-0928(9)', time: '2025-03-14 15:00' },
        // { status: 'processed', content: '成员【数据提供商一】邀请您加入项目【快捷项目-20241030145639】', time: '2025-03-27 21:48:10' },
        // { status: 'processing', content: '成员【数据提供商一】申请使用结构化数据集：lb-测试数据A-0928(9)', time: '2024-11-26' },
        // { status: 'unread', content: '成员【数据提供商三】通过同意将加密模型参数集"花瓣特征V2(300)"添加到项目：projectX-202503', time: '2025-03-26 14:30' },
        // { status: 'read', content: '成员【数据提供商四】已同意加入联盟链协作项目【blockchainFL-0325】', time: '2025-03-25 11:00' },
        // { status: 'unread', content: '智能合约自动验证通过项目【FL-testnet-098】的数据集合规性', time: '2025-03-24 08:45' },
        // { status: 'unread', content: '成员【数据提供商五】请求共享同态加密参数用于项目：医疗影像分析V3', time: '2025-03-29 14:00' },
        // { status: 'read', content: '共识节点验证通过第2356号联邦学习任务的训练结果', time: '2025-03-28 18:30' }
      ],
      notificationTab: 'all',
      notifications: [
        { status: 'unread', content: '成员【节点05】已同意加入项目【CNN模型训练】', time: '2024-12-29 10:25' },
        { status: 'read', content: '成员【数据提供商一】已同意加入项目【RESNET10模型训练】', time: '2025-03-28 15:30' },
        { status: 'unread', content: '成员【节点01】已同意加入项目【ResNet10模型训练】', time: '2025-01-03 09:45' },
        { status: 'unread', content: '成员【节点06】已同意加入项目【CNN模型训练】', time: '2025-01-03 16:11' },
        { status: 'read', content: '成员【数据提供商二】通过同意将结构化数据集“横纵混合C全(17518)”添加到项目：test06-横向联邦', time: '2025-03-28 15:30' },
        { status: 'unread', content: '成员【节点02】已同意加入项目【CNN模型训练】', time: '2025-01-16 18:01' },
        { status: 'processing', content: '成员【数据提供商六】申请使用联邦学习框架v2.3.1的API接口权限', time: '2025-03-16 09:00' },
        { status: 'processed', content: '联盟链网关完成项目【FL-cross-045】的资产映射', time: '2025-03-26 16:48:20' },
        { status: 'processing', content: '成员【数据提供商七】请求配置隐私计算节点用于金融风控模型', time: '2025-03-18 15:30' },
        { status: 'processing', content: '审计节点发起对项目【gov-data-2025】的合规性检查', time: '2025-03-20 10:00' },
        { status: 'processed', content: '共识层完成第4567号智能合约的部署', time: '2025-03-27 09:45:30' }
      ],
     processingDialogVisible: false,
      detailDialogVisible: false,
      currentTodo: {}
    };
  },
  computed: {
    filteredTodos() {
      if (this.currentTab === 'all') return this.todos;
      if (this.currentTab === 'processing')
        return this.todos.filter(item => item.status === 'processing');
      return this.todos.filter(item => item.status === 'processed');
    },
    filteredNotifications() {
      if (this.notificationTab === 'all') return this.notifications;
      if (this.notificationTab === 'unread')
        return this.notifications.filter(item => item.status === 'unread');
      return this.notifications.filter(item => item.status === 'read');
    }
  },
  methods: {
    // 待办事项处理
    handleProcess(todo) {
      this.currentTodo = {
        ...todo,
        handler: '系统自动分配',
        processTime: '',
        remark: ''
      };
      this.processingDialogVisible = true;
    },

    submitProcessing() {
      // 模拟异步处理
      setTimeout(() => {
        this.currentTodo.status = 'processed';
        this.currentTodo.processTime = new Date().toISOString().substr(0, 19).replace('T', ' ');
        this.todos = this.todos.map(item => item.id === this.currentTodo.id ? this.currentTodo : item);
        this.processingDialogVisible = false;
        this.$message.success('处理完成！');
      }, 1000);
    },

    resetProcessingForm() {
      this.currentTodo = {};
    },

    // 查看详情
    handleViewTodoDetails(todo) {
      this.currentTodo = { ...todo };
      this.detailDialogVisible = true;
    },

    // 通知处理
    handleMarkAsRead(notification) {
      notification.status = 'read';
      this.$message.info('已标记为已读');
    },

    handleViewNotificationDetails(notification) {
      // 这里可以添加通知详情的对话框或页面跳转
      console.log('查看通知详情:', notification);
      this.$message.info('查看通知详情');
    }
  }
};
</script>

<style scoped>
/* 滚动条样式 */
.todo-list {
  list-style: none;
  padding: 0;
  min-height: calc(100vh - 10px);
  /* 根据视口高度调整 */
  /* 固定列表高度 */
  overflow-y: auto;
  /* 垂直滚动条 */
  margin: 0 -12px;
  /* 抵消内边距 */
}

.todo-item {
  margin-bottom: 10px;
  padding: 8px;
  background: white;
  border-radius: 4px;
  display: flex;
  align-items: center;
  gap: 16px;
}

.status-tag {
  padding: 2px 8px;
  border-radius: 4px;
  flex-shrink: 0;
  width: 70px;
  text-align: center;
}

.content {
  flex: 1;
  min-width: 0;
  line-height: 1.5;
  padding-right: 10px;
  text-overflow: ellipsis;
  white-space: nowrap;
  overflow: hidden;
}

.handle-btn {
  color: #409eff;
  cursor: pointer;
  flex-shrink: 0;
  margin-left: auto;
}

.time {
  color: #999;
  font-size: 12px;
  flex-shrink: 0;
  width: 140px;
  text-align: right;
}

/* 其他原有样式保留 */
.todo-container {
  flex-basis: 50%;
  /* 各占50%宽度 */
  box-sizing: border-box;
  /* 包含padding计算宽度 */
  min-width: 300px;
  /* 防止过小屏幕内容溢出 */
  min-height: calc(100vh - 10px);
  /* 根据视口高度调整 */
  border-radius: 10px;
  box-shadow: 0 4px 16px rgba(0, 0, 0, 0.08);
  border: none;
  transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
}

.todo-container:hover {
  transform: translateY(-2px);
  box-shadow: 0 8px 24px rgba(0, 0, 0, 0.12);
}

.tab-bar {
  margin-bottom: 20px;
}

.tab-item {
  display: inline-block;
  padding: 8px 16px;
  margin-right: 10px;
  cursor: pointer;
  border-radius: 4px;
}

.tab-item.active {
  background: #409eff;
  color: white;
}

.processing {
  background: #fff9db;
  color: #f5a623;
}

.processed {
  background: #e8f6e8;
  color: #67c23a;
}

.card-header {
  background: linear-gradient(135deg, #f8f9fa 0%, #f1f3f5 100%);
  border-bottom: 1px solid #e9ecef !important;
  padding: 5px 5px !important;
}

.header-content {
  display: flex;
  align-items: center;
  gap: 12px;
}

.header-icon {
  font-size: 20px;
  color: #409EFF;
  padding: 8px;
  background: rgba(64, 158, 255, 0.1);
  border-radius: 8px;
}

.header-title {
  font-size: 18px;
  font-weight: 600;
  color: #2c3e50;
  letter-spacing: 0.5px;
}

.todo-count {
  margin-left: auto;
  font-size: 14px;
  color: #6c757d;
  background: rgba(108, 117, 125, 0.08);
  padding: 4px 10px;
  border-radius: 12px;
}

.status-tag.unread {
  background: #fff9db;
  color: #f5a623;
}

.status-tag.read {
  background: #e8f6e8;
  color: #67c23a;
}

/* 合作通知图标样式 */
.el-icon-chat-dot-square {
  color: #2563eb;
  background: rgba(37, 99, 235, 0.1);
}

/* 布局调整 */
.dashboard-container {
  display: flex;
  flex-direction: row;
  gap: 20px;
  padding: 20px;
  flex-wrap: nowrap;
  /* 禁止换行 */
}
</style>